/*     */ package com.zimbra.cs.util.yauth;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.NameValuePair;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RawAuth
/*     */   implements Auth
/*     */ {
/*     */   private final String appId;
/*     */   private String cookie;
/*     */   private String wssId;
/*     */   private long expiration;
/*  46 */   private static final Logger LOG = Logger.getLogger(RawAuth.class);
/*     */   
/*     */ 
/*     */   private static final boolean DEBUG = false;
/*     */   
/*     */   private static final String GET_AUTH_TOKEN = "get_auth_token";
/*     */   
/*     */   private static final String GET_AUTH = "get_auth";
/*     */   
/*     */   private static final String LOGIN = "login";
/*     */   
/*     */   private static final String PASSWD = "passwd";
/*     */   
/*     */   private static final String APPID = "appid";
/*     */   
/*     */   private static final String TOKEN = "token";
/*     */   
/*     */   private static final String AUTH_TOKEN = "AuthToken";
/*     */   
/*     */   private static final String COOKIE = "Cookie";
/*     */   
/*     */   private static final String WSSID = "WSSID";
/*     */   
/*     */   private static final String EXPIRATION = "Expiration";
/*     */   
/*     */   private static final String ERROR = "Error";
/*     */   
/*     */   private static final String ERROR_DESCRIPTION = "ErrorDescription";
/*     */   
/*     */   private static final String CAPTCHA_URL = "CaptchaUrl";
/*     */   
/*     */   private static final String CAPTCHA_DATA = "CaptchaData";
/*     */   
/*     */   private static final long EXPIRATION_LIMIT = 60000L;
/*     */   
/*     */ 
/*     */   public static String getToken(String appId, String user, String pass)
/*     */     throws AuthenticationException, IOException
/*     */   {
/*  85 */     debug("Sending getToken request: appId = %s, user = %s", new Object[] { appId, user });
/*  86 */     Response res = doGet("get_auth_token", new NameValuePair[] { new NameValuePair("appid", appId), new NameValuePair("login", user), new NameValuePair("passwd", pass) });
/*     */     
/*     */ 
/*     */ 
/*  90 */     String token = res.getRequiredField("AuthToken");
/*  91 */     debug("Got getToken response: token = %s", new Object[] { token });
/*  92 */     return token;
/*     */   }
/*     */   
/*     */   public static RawAuth authenticate(String appId, String token) throws AuthenticationException, IOException
/*     */   {
/*  97 */     debug("Sending authenticate request: appId = %s, token = %s", new Object[] { appId, token });
/*  98 */     RawAuth auth = new RawAuth(appId);
/*  99 */     auth.authenticate(token);
/* 100 */     debug("Got authenticate response: %s", new Object[] { auth });
/* 101 */     return auth;
/*     */   }
/*     */   
/*     */   private RawAuth(String appId) {
/* 105 */     this.appId = appId;
/*     */   }
/*     */   
/*     */   public String getAppId()
/*     */   {
/* 110 */     return this.appId;
/*     */   }
/*     */   
/*     */   public String getCookie()
/*     */   {
/* 115 */     return this.cookie;
/*     */   }
/*     */   
/*     */   public String getWSSID()
/*     */   {
/* 120 */     return this.wssId;
/*     */   }
/*     */   
/*     */   public boolean isExpired()
/*     */   {
/* 125 */     return System.currentTimeMillis() + 60000L > this.expiration;
/*     */   }
/*     */   
/*     */   private void authenticate(String token) throws AuthenticationException, IOException
/*     */   {
/* 130 */     Response res = doGet("get_auth", new NameValuePair[] { new NameValuePair("appid", this.appId), new NameValuePair("token", token) });
/*     */     
/* 132 */     this.cookie = res.getRequiredField("Cookie");
/* 133 */     this.wssId = res.getRequiredField("WSSID");
/* 134 */     String s = res.getRequiredField("Expiration");
/*     */     try {
/* 136 */       this.expiration = (System.currentTimeMillis() + Long.parseLong(s) * 1000L);
/*     */     } catch (NumberFormatException e) {
/* 138 */       throw new IOException("Invalid integer value for field 'Expiration': " + s);
/*     */     }
/*     */   }
/*     */   
/*     */   private static Response doGet(String action, NameValuePair... params)
/*     */     throws AuthenticationException, IOException
/*     */   {
/* 145 */     String uri = LC.yauth_baseuri.value() + '/' + action;
/* 146 */     GetMethod method = new GetMethod(uri);
/* 147 */     method.setQueryString(params);
/* 148 */     int rc = HttpClientUtil.executeMethod(method);
/* 149 */     Response res = new Response(method);
/* 150 */     String error = res.getField("Error");
/*     */     
/*     */ 
/* 153 */     if ((rc == 200) && (error == null)) {
/* 154 */       return res;
/*     */     }
/* 156 */     if (rc == 999)
/*     */     {
/* 158 */       throw new AuthenticationException(ErrorCode.TEMP_ERROR, "Unable to process request at this time");
/*     */     }
/*     */     
/* 161 */     ErrorCode code = error != null ? ErrorCode.get(error) : ErrorCode.GENERIC_ERROR;
/*     */     
/* 163 */     String description = res.getField("ErrorDescription");
/* 164 */     if (description == null) {
/* 165 */       description = code.getDescription();
/*     */     }
/* 167 */     AuthenticationException e = new AuthenticationException(code, description);
/* 168 */     e.setCaptchaUrl(res.getField("CaptchaUrl"));
/* 169 */     e.setCaptchaData(res.getField("CaptchaData"));
/* 170 */     throw e;
/*     */   }
/*     */   
/*     */   private static class Response {
/*     */     final Map<String, String> attributes;
/*     */     
/*     */     Response(GetMethod method) throws IOException {
/* 177 */       RawAuth.debug("Response status: %s", new Object[] { method.getStatusLine() });
/* 178 */       this.attributes = new HashMap();
/* 179 */       InputStream is = null;
/*     */       try {
/* 181 */         is = method.getResponseBodyAsStream();
/* 182 */         BufferedReader br = new BufferedReader(new InputStreamReader(is, method.getResponseCharSet()));
/*     */         
/*     */         String line;
/* 185 */         while ((line = br.readLine()) != null) {
/* 186 */           RawAuth.debug("Response line: %s", new Object[] { line });
/* 187 */           int i = line.indexOf('=');
/* 188 */           if (i != -1) {
/* 189 */             String name = line.substring(0, i);
/* 190 */             String value = line.substring(i + 1);
/* 191 */             this.attributes.put(name.toLowerCase(), value);
/*     */           }
/*     */         }
/*     */       } finally {
/* 195 */         ByteUtil.closeStream(is);
/*     */       }
/*     */     }
/*     */     
/*     */     String getRequiredField(String name) throws IOException {
/* 200 */       String value = getField(name);
/* 201 */       if (value == null) {
/* 202 */         throw new IOException("Response missing required '" + name + "' field");
/*     */       }
/* 204 */       return value;
/*     */     }
/*     */     
/*     */     String getField(String name) {
/* 208 */       String s = (String)this.attributes.get(name.toLowerCase());
/* 209 */       if (s != null) {
/* 210 */         s = s.trim();
/* 211 */         if (s.length() > 0) {
/* 212 */           return s;
/*     */         }
/*     */       }
/* 215 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String toString()
/*     */   {
/* 225 */     return super.toString();
/*     */   }
/*     */   
/*     */   private static void debug(String fmt, Object... args)
/*     */   {
/* 230 */     LOG.debug(String.format(fmt, args));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/yauth/RawAuth.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */